package com.bjb.vr.common.utils;

import cn.hutool.core.net.NetUtil;
import cn.hutool.http.useragent.Browser;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/4/20 15:42
 */
public class InetUtil {

    private static String regex = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)";
    private static String regex2 = "\\(+.+?\\)";
    private static Pattern pattern = Pattern.compile(regex);
    private static Pattern pattern2 = Pattern.compile(regex2);


    /**
     * 获取操作系统版本
     *
     * @param userAgent
     * @return
     */
    public static String getOs(String userAgent) {
        Matcher m = pattern.matcher(userAgent);
        String result = null;
        if (m.find()) {
            result = m.group(0);
        }

        Matcher m2 = pattern2.matcher(result);
        if (m2.find()) {
            result = m2.group(0);
        }
        result = result.replace("(", "");
        result = result.replace(")", "");
        result = result.replace(" U;", "");
        result = result.replace(" zh-cn;", "");
        result = result.split(";")[0];
        return result;
    }

    /**
     * 获取内网地址
     *
     * @return
     */
    public static String getHostAddres() {
        InetAddress localhost = NetUtil.getLocalhost();
        return localhost.getHostAddress();
    }

    /**
     * 获取外网地址
     *
     * @return
     */
    public static String getIp() {
        Document document = null;
        try {
            document = Jsoup.connect("http://ip.chinaz.com/").get();
            String ipStr = document.select("#leftinfo > div.IcpMain02.bor-t1s02 > div.WhoIpWrap.jspu > div.WhwtdWrap.bor-b1s.col-gray03 > span:nth-child(1)").text();
            return ipStr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取所在城市
     *
     * @return
     */
    public static String getCity() {
        Document document = null;
        try {
            document = Jsoup.connect("http://ip.chinaz.com/").get();
            String city = document.select("#leftinfo > div.IcpMain02.bor-t1s02 > div.WhoIpWrap.jspu > div.WhwtdWrap.bor-b1s.col-gray03 > span:nth-child(4) > em").text();
            city = city.split(" ")[0];
            return city;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取运营商
     *
     * @return
     */
    public static String getSupporter() {
        Document document = null;
        try {
            document = Jsoup.connect("http://ip.chinaz.com/").get();
            String supporter = document.select("#leftinfo > div.IcpMain02.bor-t1s02 > div.WhoIpWrap.jspu > div.WhwtdWrap.bor-b1s.col-gray03 > span:nth-child(4) > em").text();
            supporter = supporter.split(" ")[1];
            return supporter;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取浏览器信息
     *
     * @param userAgentString
     * @return
     */
    public static String getBrowser(String userAgentString) {
        UserAgent userAgent = UserAgentUtil.parse(userAgentString);
        Browser browser = userAgent.getBrowser();
        return browser.getName();
    }

    /**
     * 获取浏览器版本
     *
     * @param userAgentString
     * @return
     */
    public static String getBrowserVersion(String userAgentString) {
        UserAgent userAgent = UserAgentUtil.parse(userAgentString);
        String version = userAgent.getVersion();
        return version;
    }

}
